import json
import logging
from Scripts.api import Api
from Scripts.headers import config, log_format
from Scripts.mq import conn as conn_mq

mq = conn_mq(virtual=config.get('mq', 'password_virtual_host'))

# 声明队列，参数为队列名
mq.queue_declare(config.get('mq', 'password_queue'))
logging.basicConfig(level=logging.WARNING, format=log_format)


# 消息处理函数，执行完成才说明接收完成，此时才可以接收下一条，串行
def change_password(v1, v2, v3, body_get):
    logging.info(f'得到的数据为：{v1}, {v2}, {v3},'
                 f' {json.dumps(json.loads(body_get), indent=4, ensure_ascii=False)}')
    get_value = json.loads(body_get)
    try:
        get_value_type = get_value["action"]["value"]["key"]

        # 获取json中的en_name内容
        user_id = get_value["user_id"]
        logging.info(f'获得按键类型：{get_value_type},按键user_id：{user_id}')

        if user_id:
            jira_action = Api(en_name='test', user_id=user_id)
            if get_value_type == "change_password":
                # 随机修改用户密码
                jira_action.password_change()
            else:
                logging.warning(f'按键类型未找到预先值，操作失败。')
    except Exception as e:
        logging.warning(f'未找到相关值，操作失败{e}')


mq.basic_consume(config.get('mq', 'password_queue'), change_password, True)

# 开始接收消息
mq.start_consuming()
